package com.naizhuai.ui.ainote;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naizhuai.Model.db.ainote.Note;
import com.naizhuai.R;
import com.naizhuai.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import rx.Observable;
import rx.subjects.PublishSubject;

/**
 * @author：lichenxi
 * @date 2018/3/5 10
 * email：525603977@qq.com
 * Fighting
 */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {

    private List<Note> mNoteList;
    private Context mContext;
    private Note.NoteFilterType mNoteFilterType= Note.NoteFilterType.TITLE;

    private int selectedCount = 0;

    private final PublishSubject<Note> onChangeSelectedSubject = PublishSubject.create();

    public Observable<Note> getSelectedClicks() {
        return onChangeSelectedSubject;
    }


    private final PublishSubject<Note> clickSubject = PublishSubject.create();
    public Observable<Note> getClickSubject() {
        return clickSubject;
    }

    public void setFilterType(Note.NoteFilterType noteFilterType) {
        mNoteFilterType = noteFilterType;
    }

    public NoteAdapter(Context context) {
        mContext = context;
        mNoteList=new ArrayList<>();
    }
    public boolean selecting() {
        return selectedCount > 0;
    }

    public int getSelectedCount() {
        return selectedCount;
    }

    public void clearSelected() {
        for (int i = 0; i < mNoteList.size(); i++)
            if (mNoteList.get(i).setSelected(false))
                    notifyItemChanged(i);
        selectedCount = 0;
        onChangeSelectedSubject.onNext(null);
    }

    public void allSelected() {
        for (int i = 0; i < mNoteList.size(); i++)
            if (mNoteList.get(i).setSelected(true))
                notifyItemChanged(i);
        selectedCount =mNoteList.size() ;
        onChangeSelectedSubject.onNext(null);
    }

    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        View View = mInflater.inflate(R.layout.note_list_item, parent, false);
        return new NoteHolder(View);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteHolder holder, int position) {

        final Note note=mNoteList.get(position);
         if (note.isSelected()){
             holder.cardLayout.setBackgroundColor(mContext.getResources().getColor(R.color.colorPrimary));
         }else {
             holder.cardLayout.setBackgroundColor(Color.parseColor("#00000000"));
         }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selecting()){
                    notifySelected(note.toggleSelected());
                    notifyItemChanged(position);
                    onChangeSelectedSubject.onNext(note);
                }else {
                    clickSubject.onNext(note);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                notifySelected(note.toggleSelected());
                notifyItemChanged(position);
                onChangeSelectedSubject.onNext(note);
                return true;
            }
        });
        holder.noteTitle.setText(note.title);
        holder.noteContent.setText(note.content);
        switch (mNoteFilterType){
            case CREATION:
                holder.noteDate.setText("创建于"+TimeUtils.date2String(note.getLast_update()));
                break;
            case TITLE:
            case LAST_UPDATE:
            default:
                holder.noteDate.setText("更新于"+TimeUtils.date2String(note.getLast_update()));
        }

    }

    private void notifySelected(boolean increase) {
        selectedCount += increase ? 1 : -1;
    }
    @Override
    public int getItemCount() {
        return mNoteList.size();
    }

    public ArrayList<Note>  getSelected(){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            return new ArrayList<Note>(mNoteList.stream().filter(Note::isSelected).collect(Collectors.toList()));
        }else {
             ArrayList<Note> arrayList=new ArrayList<>(selectedCount);
             for (Note note:mNoteList){
                 if (note.isSelected()){
                     arrayList.add(note);
                 }
             }
             return  arrayList;
        }
    }

    public void addNotes(List<Note> List){
        mNoteList.clear();
        mNoteList.addAll(List);
        notifyDataSetChanged();
    }
    private void remote(int position){
        mNoteList.remove(position);
        notifyItemRemoved(position);
        if (position != mNoteList.size()) {
            notifyItemRangeChanged(position, mNoteList.size() - position);
        }
    }
    public void removeSelect() {
        List<Note> selectedItems = getSelected();
        for (int i = selectedItems.size() - 1; i >= 0; i--) {
             int index=mNoteList.indexOf(selectedItems.get(i));
             remote(index);
        }

    }


    public class NoteHolder extends RecyclerView.ViewHolder {

        TextView noteTitle;

        TextView noteContent;

        TextView noteDate;

        LinearLayout cardLayout;
        public NoteHolder(View itemView) {
            super(itemView);
            noteTitle=itemView.findViewById(R.id.note_title);
            noteContent=itemView.findViewById(R.id.note_content);
            noteDate=itemView.findViewById(R.id.note_date);
            cardLayout=itemView.findViewById(R.id.card_layout);
        }

    }
}
